package com.tyk.bookstore.back.member.service.impl;


import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.common.core.util.ObjUtil;
import com.tyk.bookstore.back.common.dal.mapper.dto.SinDto;
import com.tyk.bookstore.back.member.model.dto.Member4CommentDto;
import com.tyk.bookstore.back.member.model.dto.Member4PaymentDto;
import com.tyk.bookstore.back.member.model.dto.MemberNameDto;
import com.tyk.bookstore.back.member.model.entity.MemberDetailDo;
import com.tyk.bookstore.back.member.model.result.MemberResultEnum;
import com.tyk.bookstore.back.member.repository.MemberDetailRepository;
import com.tyk.bookstore.back.member.repository.MemberRepository;
import com.tyk.bookstore.back.member.service.MemberApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 会员管理对内暴露接口
 */
@Service
@RequiredArgsConstructor
public class MemberApiServiceImpl implements MemberApiService {
    private final MemberRepository memberRepository;
    private final MemberDetailRepository memberDetailRepository;

    @Override
    public String getNicknameById(Long id) {
        SinDto<String> dto = memberDetailRepository.selectSin1Eq(
                MemberDetailDo::getNickname,
                MemberDetailDo::getMemberId, id,
                MemberDetailDo.class
        );
        AssertUtil.notNull(dto, MemberResultEnum.MEMBER_NOT_FOUND);
        return dto.getV1();
    }

    @Override
    public List<MemberNameDto> getMemberNameDtoListByIdSet(Set<Long> idSet) {
        if (idSet.isEmpty()) return new ArrayList<>();
        List<MemberNameDto> dtoList = memberDetailRepository.selectNickNameListByMemberIdSet(idSet);
        AssertUtil.equal(idSet.size(), dtoList.size(), MemberResultEnum.MEMBER_NOT_FOUND);
        return dtoList;
    }

    @Override
    public String getMemberNameById(Long memberId) {
        MemberDetailDo memberDetailDo = memberDetailRepository.selectById(memberId);
        return memberDetailDo.getNickname();
    }

    @Override
    public List<Member4CommentDto> getMember4CommentDtoListByIdList(List<Long> memberIdList) {
        if (ObjUtil.isEmpty(memberIdList)) {
            return new ArrayList<>();
        }
        List<Member4CommentDto> mdList = memberDetailRepository.selectMember4CommentDtoListByIdList(memberIdList, memberIdList.size());
        AssertUtil.equal(mdList.size(), memberIdList.size(), MemberResultEnum.MEMBER_NOT_FOUND);
        return mdList;
    }

    @Override
    public List<BinVo> getMemberNameListByIdSet(Set<Long> idSet) {
        if (idSet.isEmpty()) return new ArrayList<>();
        return memberDetailRepository
                .<BigInteger, String, Long>selectBinList1In(
                        MemberDetailDo::getMemberId, MemberDetailDo::getNickname,
                        MemberDetailDo::getMemberId, idSet,
                        MemberDetailDo.class
                )
                .stream().map(it -> new BinVo(it.getV1().longValue(), it.getV2())).toList();
    }

    @Override
    public List<Long> getIdListByNicknameLike(String nickname) {
        return memberDetailRepository.<BigInteger>selectSinList1Like(
                MemberDetailDo::getMemberId,
                MemberDetailDo::getNickname, nickname,
                MemberDetailDo.class
        ).stream().map(it -> it.getV1().longValue()).toList();
    }

    @Override
    public List<Member4PaymentDto> getMember4PaymentDtoListByIdSet(Set<Long> idSet) {
        if (ObjUtil.isEmpty(idSet)) return new ArrayList<>();
        return memberRepository.selectMember4PaymentDtoListByIdSet(idSet, idSet.size());
    }

    @Override
    public List<Long> getIdListByKw(String kw) {
        return memberRepository.selectIdListByKw(kw);
    }

}
